***** Time series migration graph (Fig 1)
use "$data/NPL_indiv_data.dta", clear

* Flag people that are ever home
gen home_post = home*(round<8)
bys hhid pid: egen ever_home = max(home_post)

* Flag peopled that recalled a migration history
gen temp = round>8
by hhid pid: egen byte recalled = max(temp)
drop temp

* Replace the (hunger) recall date with holiday date for recall rounds
replace round_date = recall_date if round>8

* Regress migration rate for prime-age men who are ever home and recall their history
xtset pid
local sample "male_prime & ever_home & recalled" 
xtreg away ib12.round if `sample', fe vce(cluster hhid)
predict b_mig if `sample' & !missing(away), xb
predict se_mig if `sample' & !missing(away), stdp

collapse round_date b_mig se_mig, by(round) fast
replace round_date = td(01jun2020) if round==7 | round==12	// to look pretty
gen hi_mig = b_mig + 1.96*se_mig
gen lo_mig = b_mig - 1.96*se_mig
sort round_date

* Make graph
	* Horizontal periods
	loc start = date("1 September, 2019", "DMY")
	loc end = date("1 June, 2020", "DMY")
	loc ricestart = date("11 Oct 2019", "DMY")
	loc riceend = date("15 Nov 2019", "DMY")
	loc wheatstart = date("8 Apr 2020", "DMY")
	loc wheatend = date("15 May 2020", "DMY")
	loc ricetext = `ricestart' + .5*(`riceend'-`ricestart')
	loc wheattext = `wheatstart' + .5*(`wheatend'-`wheatstart')
	loc ctext = td(05mar2020)
		
	* Set top and bottom of graphs
	local ytop 0.4
	quietly sum hi_mig
	local hi = `r(max)'	
	quietly sum lo_mig
	local lo = `r(min)'
	loc bot = min(1.05*`lo',0)
	local scale = 0.01*(`hi' - `bot')
	local top = max(`hi' + `ytop'*`scale',`ytop')
	loc ctop = `top'-4*`scale'
	loc ctexttop = `top'-2*`scale'
	loc htexttop = `top'-4* `scale'
	
	graph set window fontface "CMU Serif"
	gl grset 
	
	twoway (scatteri `bot' `ctext' `ctop' `ctext', recast(line) lcolor(gs0) lpattern(shortdash)) ///
		(scatteri `top' `ricestart'  `top' `riceend',  recast(area) color(navy%15) lwidth(none)) ///
		(scatteri `top' `wheatstart' `top' `wheatend', recast(area) color(navy%15) lwidth(none)) ///
		(rcap lo_mig hi_mig round_date if round>8, lcolor(gs10) msize(small) lwidth(thin)) ///
		(scatter b_mig round_date if round>8, mcolor(gs10)) ///
		(rcap lo_mig hi_mig round_date if round<8, lcolor(gs0) msize(small) lwidth(thin)) ///
		(scatter b_mig round_date if round<8, mcolor(gs0)), ///
		graphregion(color(white)) plotregion(margin(sides)) ///	
		xlabel(`start'(30.7)`end', angle(45) format(%tdm)) ///
		ytitle("Temporary Migration Rate (Men Age 16{&minus}65)", axis(1)) ylabel(0(0.1)0.4,glcolor(gs12%25) angle(0) format(`yform')) /// 
		text(`htexttop' `ricetext' "Rice" "Harvest") text(`htexttop' `wheattext' "Wheat" "Harvest") ///
		text(`ctexttop' `ctext' "COVID-19") legend(on ring(0) position(7) order(7 "2019{&minus}2020" 5 "2018{&minus}2019") cols(1))
	graph export "$figures/Figure_1.png", replace

	
	
***** Time series remittance graphs (Figs 4C, 4D)
use "$data/NPL_hh_data.dta", clear

* Collapse means and standard errors by survey round
keep if round<8
xtset hhno
foreach yvar of varlist remit remit_pc {
	* Residualized after HH FEs
	xi: xtreg `yvar' i.round, fe cluster(hhno)
	predict mu_`yvar' if !missing(`yvar'), xb
	predict se_`yvar' if !missing(`yvar'), stdp
}

collapse round_date mu_* se_*, by(round) fast
replace round_date = td(01jun2020) if round==7	// to look pretty
foreach outcome  of varlist mu_* {
	local yvar = substr("`outcome'",4,.)
	gen hi_`yvar' = mu_`yvar' + 1.96*se_`yvar'
	gen lo_`yvar' = mu_`yvar' - 1.96*se_`yvar'
}
sort round_date

* Make graphs
graph set window fontface "CMU Serif"
foreach outcome in remit remit_pc {
	if "`outcome'"=="remit" {
		local yname "Total Remittance Income (USD PPP)"
		local filename "4C"
	}
	else if "`outcome'"=="remit_pc" {
		local yname "Remittances per Adult Male Away (USD PPP)"
		local filename "4D"
	}
	local ylabs = `"0 "0" 50 "50" 100 "100" 150 "150" 200 "200" "'
	local ytop = 10
		
	* Horizontal periods
	loc start = date("1 September, 2019", "DMY")
	loc end = date("1 June, 2020", "DMY")
	loc ricestart = date("11 Oct 2019", "DMY")
	loc riceend = date("15 Nov 2019", "DMY")
	loc wheatstart = date("8 Apr 2020", "DMY")
	loc wheatend = date("15 May 2020", "DMY")
	loc ricetext = `ricestart' + .5*(`riceend'-`ricestart')
	loc wheattext = `wheatstart' + .5*(`wheatend'-`wheatstart')
	loc ctext = td(05mar2020)
		
	* Set top and bottom of graphs
	quietly sum hi_remit
	local hi = `r(max)'	
	quietly sum lo_`yvar'
	local lo = `r(min)'
	loc bot = min(1.05*`lo',0)
	local scale = 0.01*(`hi' - `bot')
	local top = `hi' + `ytop'*`scale'
	loc ctop = `hi' + (`ytop'-4.5)*`scale'
	loc ctexttop = `hi' + (`ytop'-2.5)*`scale'
	loc htexttop = `hi' + (`ytop'-4.5)* `scale'
	
	if `bot'<0 {
		local seasons (scatteri `bot' `wheatstart' `bot' `wheatend', recast(area) color(navy%15) lwidth(none)) ///
					(scatteri `bot' `ricestart'  `bot' `riceend',  recast(area) color(navy%15) lwidth(none))
	}
	else local seasons ""
	
	twoway (scatteri `bot' 21977 `ctop' 21977, recast(line) lcolor(gs0) lpattern(shortdash)) ///
		(scatteri `top' `ricestart'  `top' `riceend',  recast(area) color(navy%15) lwidth(none)) ///
		(scatteri `top' `wheatstart' `top' `wheatend', recast(area) color(navy%15) lwidth(none)) ///
		`seasons' (rcap lo_`outcome' hi_`outcome' round_date, lcolor(gs0) msize(small) lwidth(thin)) ///
		(scatter mu_`outcome' round_date, mcolor(gs0)), graphregion(color(white)) plotregion(margin(sides)) ///	
		xlabel(`start'(30.7)`end', angle(45) format(%tdm)) ///
		ytitle("`yname'", axis(1)) ylabel(`ylabs',glcolor(gs12%25) angle(0) format(`yform')) /// 
		text(`htexttop' `ricetext' "Rice" "Harvest") text(`htexttop' `wheattext' "Wheat" "Harvest") ///
		text(`ctexttop' `ctext' "COVID-19") legend(off)

	graph export "$figures/Figure_`filename'.png", replace
}



***** Difference-in-differences income graphically (Fig 4A)
use "$data/NPL_hh_data.dta", clear

* Flag balanced panel
forvalues i = 3/6 {
	gen temp = round==`i'
	bys hhno: egen has`i' = max(temp)
	drop temp
}

* Compute average income by round and migration status
xtset hhno
xi: xtreg laborInc highRemit##i.round if has3 & has6, fe vce(cluster hhno)
predict b, xb
predict s, stdp
collapse b s, by(round highRemit) fast
keep if inlist(round, 3, 6)

* Make Graph (Figure 4A)
gen group = 2-highRemit if round==3
replace group = 4.25-highRemit if round==6

gen se_hi = b+1.96*s
gen se_lo = b-1.96*s

local pre "October 2019 (Rice Harvest)"
local post "April 2020 (Wheat Harvest)"
local mig "High Remittance"
local nomig "Low Remittance"
local ylab "Monthly Non-Farm Income (USD PPP)"

graph set window fontface "CMU Serif"

twoway  (bar b group if highRemit==0 , fcolor(gs14) lcolor(gs0) barw(1)) ///
		(bar b group if highRemit==1 , fcolor(red*0.9) lcolor(gs0) barw(1)) ///
		(rcap se_lo se_hi group, lcolor(gs0) msize(small) lwidth(thin)) ///
		(scatteri 0 2, color(none)), graphregion(color(white)) ///
		xlabel(1.5 "`pre'" 3.75 "`post'", notick) xtitle("") ylabel(0(100)400) ///
		legend(order(2 "`mig'" 1 "`nomig'")) ytitle("`ylab'") plotregion(margin(sides))

graph export "$figures/Figure_4A.png", replace


***** Food Insecurity Event Studies graphically (Figs 4B and 6A)
use "$data/NPL_hh_data.dta", clear

* Households with recall hunger data
gen byte recall = round>8
egen byte rec_hh = max(recall), by(hhid)
keep if rec_hh == 1 

* Food Insecurity split by remittance type
reg FoodIns round#highRemit, vce(cluster hhno)
forvalues i = 0/1 {
	predict b`i'_pre if recall==1 & highRemit==`i' & !missing(FoodIns), xb
	predict se`i'_pre if recall==1 & highRemit==`i' & !missing(FoodIns), stdp
	predict b`i'_post if recall==0 & highRemit==`i' & !missing(FoodIns), xb
	predict se`i'_post if recall==0 & highRemit==`i' & !missing(FoodIns), stdp
}

* Food Insecurity split by remittance x village type
reg FoodIns round#vilXrem, vce(cluster hhno)
foreach i in 00 01 10 11 {
	predict bV`i'_pre if recall==1 & vilXrem==`i' & !missing(FoodIns), xb
	predict seV`i'_pre if recall==1 & vilXrem==`i' & !missing(FoodIns), stdp
	predict bV`i'_post if recall==0 & vilXrem==`i' & !missing(FoodIns), xb
	predict seV`i'_post if recall==0 & vilXrem==`i' & !missing(FoodIns), stdp
}

collapse date=round_date recall *_pre *_post , by(round) fast
replace date = date + 365 if round>8 			// Put in same year for graphing
replace date = td(01jun2020) if round==7		// Looks better graphically
sort round

*Bump back the "July 2020" observation
replace date = date - 365 if round>7 & date > date("30 June, 2020", "DMY")

*Duplicate last recall (typical year) observation for graphing
sort date
quietly count
expand 2 in `r(N)', gen(newvar)
replace date = date - 365 if newvar==1
drop newvar

* Slightly offset migrants in the post data
gen datem = date+2

* Slightly offset mig villages in the post data
gen dateLn = date-4
gen dateLm = date-1
gen dateVm = date+4
gen dateVn = date+1
sort date

foreach s in pre post {
	foreach i in 0 1 V00 V01 V10 V11 {
		gen hi`i'_`s' = b`i'_`s' + 1.96*se`i'_`s'
		gen lo`i'_`s' = b`i'_`s' - 1.96*se`i'_`s'
	}
}

** Graph by Household Migration (Figure 4B)

*Locations of plot text
sum lo0_post
loc lo = `r(min)' 
sum hi1_post
loc th1 = `r(max)' + 0.15*(`r(max)'-`lo')
loc th2 = `r(max)' + 0.1*(`r(max)'-`lo')

*Locations of season shading
loc start = date("2 July, 2019", "DMY")
loc end = date("1 June, 2020", "DMY")
loc ricestart = date("11 Oct 2019", "DMY")
loc riceend = date("15 Nov 2019", "DMY")
loc wheatstart = date("8 Apr 2020", "DMY")
loc wheatend = date("15 May 2020", "DMY")
loc covdate = date("05 March 2020", "DMY")

sort date

local dash = char(151)
loc cis (rcap lof_post hif_post date if !recall , lcolor("0 114 178") msize(small) lwidth(medium))
loc cis_r (rarea lof_pre hif_pre date if recall , color(gs13) lwidth(0))


graph set window fontface "CMU Serif" 

loc start = date("2 July, 2019", "DMY")
loc end = date("1 June, 2020", "DMY")
loc covdate = date("05 March 2020", "DMY")
tw 	(scatteri -1 `covdate' 1.85 `covdate', recast(line) lcolor(gs0) lpattern(shortdash)) ///
	(rarea lo0_pre hi0_pre date, color(gs0%15) lwidth(0)) (rarea lo1_pre hi1_pre date, color(gs0%15) lwidth(0)) ///
	(line b0_pre date if recall, color(gs0))  ///
	(line b1_pre date if recall, color(red*.8))  ///
	(rcap lo0_post hi0_post date, color(gs0) msize(small) lwidth(thin)) ///
	(rcap lo1_post hi1_post datem, color(red*.8) msize(small) lwidth(thin)) ///
	(scatter b0_post date if !recall , mcolor(gs0) msymbol(O) )  ///
	(scatter b1_post datem if !recall , mcolor(red*.8) msymbol(T))  ///
	, graphregion(color(white)) ytitle(Food Insecurity Index) ///
	 xlabel(`start'(30.6)`end', angle(45) format(%tdm) )   ///
	 ylabel(-1 "-1" 0 "0" 1 "1" 2 "2", angle(0))   ///
	 ttext(1.94 `covdate' "COVID-19")  plotregion(margin(0)) ///
	 legend(order (9 "High Remit, 2019{&minus}2020" 8 "Low Remit, 2019{&minus}2020" 5 "High Remit, Typical Yr." 4 "Low Remit, Typical Yr."))  
	 
graph export "$figures/Figure_4B.png", replace

*****
* Graph by Household and Village Migration (Figure 6A)
*****

sort date

*Locations of plot text
sum loV01_post
loc lo = `r(min)' 
sum hiV10_post
loc th1 = `r(max)' + 0.15*(`r(max)'-`lo')
loc th2 = `r(max)' + 0.1*(`r(max)'-`lo')


loc start = date("2 July, 2019", "DMY")
loc end = date("1 June, 2020", "DMY")
graph set window fontface "CMU Serif" 
tw 	(connected bV00_post dateLn   if recall, lpattern(solid) msymbol(O) color(gs0)) ///
	(connected bV10_post dateVn if recall, lpattern(dash) msymbol(Oh) color(gs0)) ///
	(connected bV01_post dateLm  if recall, lpattern(solid) msymbol(T) color(red*.8)) ///
	(connected bV11_post dateVm if recall, lpattern(dash) msymbol(Th) color(red*.8)) ///
	(scatteri -2 21977 2.8 21977, recast(line) lcolor(gs0) lpattern(shortdash)) ///
	(rarea loV00_pre hiV00_pre date, color(gs0%15) lwidth(0)) ///
	(rarea loV01_pre hiV01_pre date, color(gs0%15) lwidth(0)) ///
	(rarea loV10_pre hiV10_pre date, color(gs0%15) lwidth(0)) ///
	(rarea loV11_pre hiV11_pre date, color(gs0%15) lwidth(0)) ///
	(line bV00_pre date if recall, color(gs0))  ///
	(line bV01_pre date if recall, color(red*.8) lpattern(solid))  ///
	(line bV10_pre date if recall, color(gs0) lpattern(dash))  ///
	(line bV11_pre date if recall, color(red*.8) lpattern(dash))  ///
	(rcap loV00_post hiV00_post dateLn, color(gs0) msize(small) lwidth(thin)) ///
	(rcap loV01_post hiV01_post dateVn, color(red*.8) msize(small) lwidth(thin)) ///
	(rcap loV10_post hiV10_post dateLm, color(gs0) msize(small) lwidth(thin)) ///
	(rcap loV11_post hiV11_post dateVm, color(red*.8) msize(small) lwidth(thin)) ///
	(scatter bV00_post dateLn if !recall , mcolor(gs0) msymbol(O))  ///
	(scatter bV01_post dateVn if !recall , mcolor(red*.8) msymbol(T))  ///
	(scatter bV10_post dateLm if !recall , mcolor(gs0) msymbol(Oh))  ///
	(scatter bV11_post dateVm if !recall , mcolor(red*.8) msymbol(Th))  ///
	, graphregion(color(white)) title(`title') ytitle(Food Insecurity Index) ///
	 xlabel(`start'(30.6)`end', angle(45) format(%tdm) )   ///
	 ylabel(-2 "-2" -1 "-1" 0 "0" 1 "1" 2 "2" 3 "3", angle(0))   ///
	 ttext(2.9 5Mar2020 "COVID-19")  plotregion(margin(0)) ///
	 legend(order(3 "High Remit, Low Village" 1 "Low Remit, Low Village" 4 "High Remit, High Village" 2 "Low Remit, High Village"))  

graph export "$figures/Figure_6A.png", replace
